<template>
  <div>
    <van-search
      background="#f6f5f6"
      v-model="value"
      show-action
      placeholder="请输入搜索关键词"
      @input="onInput"
      @cancel="onCancel"
    />
    <van-cell-group v-show="value.length">
      <van-cell :title="item.communityName" v-for="item in communityList" :key="item.community" @click="selFn(item.communityName,item.community)"/>
    </van-cell-group>
  </div>
</template>

<script>
import { communityApi } from '@/api'
export default {
  data () {
    return {
      value: '',
      communityList: []
    }
  },
  methods: {
    onInput (val) {
      this.communityList = []
      clearTimeout(this.timer)
      this.timer = setTimeout(async () => {
        const res = await communityApi({
          id: JSON.parse(localStorage.getItem('hkzf_city')).value,
          name: val
        })
        // console.log(res.data.body[0].communityName)
        this.communityList = res.data.body
        console.log(this.communityList)
      }, 500)
    },
    onCancel () {
      this.$router.back()
    },
    selFn (name, id) {
      // console.log(name)
      this.$router.push({
        name: 'Add',
        params: { name, id }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.van-search {
  padding: 12px 0;
}
/deep/.van-search__content {
  margin-left: 0;
}
/deep/ .van-search .van-cell {
  background-color: #fff;
}
</style>
